package top.continew.admin.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.admin.system.model.resp.ExportTaskResp;
import top.continew.admin.system.service.ExportTaskService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.model.R;

/**
 * 导出任务管理 API
 */
@Tag(name = "导出任务管理 API")
@RestController
@RequestMapping("/system/exportTask")
@RequiredArgsConstructor
public class ExportTaskController {

    private final ExportTaskService exportTaskService;

    @Operation(summary = "分页查询导出任务")
    @GetMapping
    public PageResp<ExportTaskResp> page(PageQuery pageQuery) {
        Long userId = UserContextHolder.getUserId();
        return exportTaskService.getUserTasksPage(userId, pageQuery);
    }

    @Operation(summary = "获取导出任务详情")
    @GetMapping("/{taskId}")
    public R<ExportTaskResp> getTaskDetail(@PathVariable String taskId) {
        return R.ok(exportTaskService.getTaskDetail(taskId));
    }

    @Operation(summary = "下载导出文件")
    @GetMapping("/download/{taskId}")
    public void downloadFile(@PathVariable String taskId, HttpServletResponse response) {
        exportTaskService.downloadFile(taskId, response);
    }
}